package biz

import (
	"context"
	"fmt"
	"github.com/go-kratos/kratos/v2/log"
	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/trace"
	"kratos_cron_task/internal/conf"
	"kratos_cron_task/internal/utils"
)

type CronInterface interface {
	CronTest1(ctx context.Context) error
	CronTest2(ctx context.Context) error
}

type CronBiz struct {
	YamlConfig *conf.Bootstrap
	Logger     *log.Helper
	CronRepo   CronInterface
}

func NewCronBiz(yamlConfig *conf.Bootstrap, logger log.Logger, cronInterface CronInterface) *CronBiz {
	return &CronBiz{
		YamlConfig: yamlConfig,
		Logger:     log.NewHelper(logger),
		CronRepo:   cronInterface,
	}
}

func (b *CronBiz) RunCronTask1Biz(ctx context.Context) error {
	ctx, span := otel.Tracer("BIZ").Start(ctx, "RunCronTask1Biz", trace.WithSpanKind(trace.SpanKindInternal))
	defer span.End()

	fmt.Println("调用了RunCronTask1Biz方法～ time: ", utils.GetCurrentTime())

	return b.CronRepo.CronTest1(ctx)
}

func (b *CronBiz) RunCronTask2Biz(ctx context.Context) error {
	ctx, span := otel.Tracer("BIZ").Start(ctx, "RunCronTask2Biz", trace.WithSpanKind(trace.SpanKindInternal))
	defer span.End()

	fmt.Println("执行了定时任务的RunCronTask2Biz方法, time: ", utils.GetCurrentTime())

	return b.CronRepo.CronTest2(ctx)
}
